<!-- 填写维修单 -->
<template>
  <div class="content">
    <avue-form
      class="avueForm"
      v-if="visible"
      v-model="formData"
      :option="config"
      @submit="requestPutTask"
    />
  </div>
</template>

<script>
import {
  putAvueForm,
  putTask,
  infoPush,
  equipment,
  getUser
} from '@/api/index'
import { Notify } from 'vant'

export default {
  data () {
    return {
      visible: true,
      formData: {},
      config: {},
      type: ''
    }
  },
  created () {
    const { formData, config, taskId, type } = this.$route.query
    // this.changeUser(formData)
    this.formData = formData
    this.taskId = taskId
    // this.formData = formData
    this.config = JSON.parse(config)
    this.type = type
  },
  components: {
    [Notify.name]: Notify
  },

  computed: {},

  methods: {
    async changeUser (formData) {
      const _form = JSON.parse(JSON.stringify(formData))
      const data = {
        idList: _form.owner
      }
      const res = await getUser(data)
      const values = Object.values(res)
      _form.owner = values[0].name
      this.formData = _form
    },
    async requestPutTask (val) {
      const data = {
        variables: val,
        complete: 1,
        taskId: this.taskId
      }
      await putAvueForm(data) // 提交数据

      if (this.type === '维修' && !val.needBackup) {
        const informData = {
          params: {
            // itemname
            itemName: val.itemName
          },
          type: 'mission_assignment'
          // category: '维修类',
          // title: '维修完成通知',
          // contentWithReplaceHolder: val.itemName + '任务已完成，请注意查收',
          // tagName: '综合业务管理人员——维修任务'
        }
        await infoPush(informData) // info 通知

        await equipment({
          equipments: [
            {
              id: val.device_id,
              stateId: val.deviceStateId
            }
          ]
        })
      } else if (this.type === '维保') {
        var list = val.device
        list.forEach((item, index) => {
          item.stateId = 1
        })
        await equipment({
          equipments: list
        })
      }

      this.$router.go(-1)
      Notify({ type: 'success', message: '提交成功' })
    },

    // 填写保修单成功
    async putTask () {
      try {
        const params = {
          taskId: '',
          variables: '',
          complete: 1
        }
        const res = await putTask(params)
        console.log(res)
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>
<style lang='less' scoped>
.content {
  width: 100%;
  height: 100%;
  background: #fff;
  > .avueForm {
    margin-top: 15px;
  }
}
</style>
